package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.list.client.ListClientFeign;
import com.atguigu.gmall.model.list.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * @Author: lx
 * @Time: 14:27
 * @Description:
 *
 *  页面微服务 上  接收  页面传递过来的参数   6部分数据
 */
@Controller
public class ListController {


    @Autowired
    private ListClientFeign listClientFeign;
    //开始搜索
    //参数1：关键词
    //参数2：品牌
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){//9个入参
    //public String list(@RequestParam Map<String,String> map){//9个入参
        SearchResponseVo searchResponseVo = listClientFeign.list(searchParam);
        //1：入参条件对象 回显
        model.addAttribute("searchParam",searchParam);
        //2:品牌集合 回显
        List<SearchResponseTmVo> trademarkList = searchResponseVo.getTrademarkList();
        model.addAttribute("trademarkList",trademarkList);
        //3:平台属性集合
        List<SearchResponseAttrVo> attrsList = searchResponseVo.getAttrsList();
        model.addAttribute("attrsList",attrsList);
        //4:商品结果集
        List<Goods> goodsList = searchResponseVo.getGoodsList();
        model.addAttribute("goodsList",goodsList);
        //5:分页
        Integer pageNo = searchResponseVo.getPageNo();
        Long totalPages = searchResponseVo.getTotalPages();
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("totalPages",totalPages);//总页数
        //预计 还有4个
        UUID.randomUUID().toString().replace("-","");
        //Thymeleaf标签
        // #string.replace(uuid,'-','')
        //6：回显品牌选中项  品牌:金龙鱼
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);

        //7:平台属性及值选中项
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        //8:URL + 入参集合
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //9: 排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        return "list/index";
    }

    //排序 回显
    private Map buildOrderMap(SearchParam searchParam) {
        Map map = new HashMap();
        String order = searchParam.getOrder();//1:desc 1:asc  2:desc 2:asc
        if(!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            map.put("type",o[0]);
            map.put("sort",o[1]);
        }else{
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    //URL + 入参集合  字符串拼接
    private String buildUrlParam(SearchParam searchParam) {
        //StringBuffer StringBuild
        StringBuilder sb = new StringBuilder();

        //http://list.gmall.com/list.html
        // ?keyword=%E6%89%8B%E6%9C%BA&trademark=1:%E8%8B%B9%E6%9E%9C
        // &props=1:4G:bb&props=2:骁龙439:CPU型号
        //URL
        //关键词   注意事项：关键词要有  拼接不正确
        String keyword = searchParam.getKeyword();
        if(!StringUtils.isEmpty(keyword)){
            sb.append("keyword=").append(keyword);
        }
        //品牌
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            if(sb.length() != 0){
                sb.append("&trademark=").append(trademark);//品牌Id:品牌名称
            }else{
                sb.append("trademark=").append(trademark);//品牌Id:品牌名称
            }
        }
        //平台
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                if(sb.length() != 0){
                    sb.append("&props=").append(prop);//props=2:骁龙439:CPU型号
                }else{
                    sb.append("props=").append(prop);//props=2:骁龙439:CPU型号
                }
            }
        }
        //排序
        //http://list.gmall.com/list.html
        // ?keyword=%E6%89%8B%E6%9C%BA&trademark=1:%E8%8B%B9%E6%9E%9C
        // &props=1:4G:bb&props=2:骁龙439:CPU型号&order=1:asc
/*        String order = searchParam.getOrder();//1:desc 1:asc  2:desc 2:asc
        if(!StringUtils.isEmpty(order)){
            if(sb.length() != 0){
                sb.append("&order=").append(order);
            }else{
                sb.append("order=").append(order);
            }
        }else{
            if(sb.length() != 0){
                sb.append("&order=").append("1:desc");
            }else{
                sb.append("order=").append("1:desc");
            }
        }*/
        return "/list.html?" + sb.toString();
    }

    //台属性及值选中项
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        List<Map> propsParamList = null;
        //平台属性参数
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            propsParamList = new ArrayList<>();//懒汉式
            for (String prop : props) {//平台ID:平台属性值:平台属性名称
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                propsParamList.add(map);
            }
        }
        return propsParamList;

    }
    //回显品牌选中项  品牌:金龙鱼
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();// 品牌ID:品牌名称
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];//硬 编码 问题
        }
        return null;
    }

    public static void main(String[] args) {
        String s = "&haha=hehe";
        char c = s.charAt(0);
        if(c == '&'){
            s = s.substring(1);
        }
        System.out.println(s);
    }

}
